import { HTTP_CONFIG } from "../config.js";
export default class Ajax {

    constructor() {

    }

    get(url, data, cb) {

        this.url = url
        this.data = (typeof data != "function" ? data : {})
        this.method = 'GET'
        this.header = { 'content-type': 'application/json' }
        this.request((typeof data == "function" ? data : cb))

    }

    post(url, data, cb) {

        this.url = url
        this.data = (typeof data != "function" ? data : {})
        this.method = 'POST'
        this.header = { 'content-type': 'application/x-www-form-urlencoded' }
        this.request((typeof data == "function" ? data : cb))

    }

    request(cb) {

        wx.request({
            url: HTTP_CONFIG.HTTP_URL + this.url,
            data: this.data,
            method: this.method,
            header: this.header,
            success: res => {

                // console.log("请求成功", res)
                cb(res)

            },
            fail: res => {

                // console.log('请求失败', res)
                cb(res)

            }
        })

    }

}